'use strict';

const {Controller} = require('egg');
const ResponseUtil = require('../utils/ResponseUtil');

class ProductController extends Controller {


    async addProduct(){
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.product.addProduct(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async editProduct(){
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.product.editProduct(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getProductList(){
        let query = this.ctx.query;
        query.delFlag = 0;
        const resp = await this.ctx.service.product.getProductList(query);
        return this.ctx.body = ResponseUtil.success(resp.list, resp.total);
    }

    async getProductDetail(){
        let productId = this.ctx.params.productId;
        if (!productId) {
            return ResponseUtil.retArgErr();
        }
        const product = await this.ctx.service.product.selectProductById(productId);

        return this.ctx.body = ResponseUtil.success(product);
    }

    async deleteProduct(){
        let productId = this.ctx.params.productId;
        if (!productId) {
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.product.deleteProduct(productId);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async updateProductStatus(){
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.product.updateProductStatus(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getProductApplyList(){
        let query = this.ctx.query;
        const resp = await this.ctx.service.product.getProductApplyList(query);
        return this.ctx.body = ResponseUtil.success(resp.list, resp.total);
    }

    async updateProductApplyImportantLevel(){
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.product.updateProductApplyImportantLevel(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

}

module.exports = ProductController;
